package com.example.edu.Service.Impl;

import com.example.edu.Dto.Return;
import com.example.edu.Entity.User;
import com.example.edu.Mapper.UserMapper;
import com.example.edu.Service.UserService;
import com.example.edu.Utils.CacheDataBaseUtil;
import com.example.edu.Utils.JwtUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.concurrent.TimeUnit;

import static com.example.edu.Utils.MyConstants.*;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    UserMapper userMapper;
    @Resource
    StringRedisTemplate stringRedisTemplate;
    @Resource
    CacheDataBaseUtil cacheDataBaseUtil;
    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Override
    @Transactional
    public Return update(HttpServletRequest request, User newMessage) {
        try {
            return update1(request,newMessage);
        }catch (Exception e){
            return Return.fail(1,"修改失败");
        }
    }

    @Override
    @Transactional
    public Return updatePassword(HttpServletRequest request, User newMessage) {
        try {
            return updatePassword1(request,newMessage);
        }catch (Exception e){
            return Return.fail(1,"修改失败");
        }
    }

    @Override
    public Return get(HttpServletRequest request) {
        String token = JwtUtil.getTokenFromCookie(request);
        String telephone = JwtUtil.getUsername(token);
        User user = cacheDataBaseUtil.selectById(USER_KEY,telephone,User.class, telephone2 -> userMapper.getUser(telephone2),USER_CACHE_TTL, TimeUnit.HOURS);
        return Return.ok(user);
    }

    @Override
    public Return logout(HttpServletRequest request) {
        String token = JwtUtil.getTokenFromCookie(request);
        String telephone = JwtUtil.getUsername(token);
        try {
            stringRedisTemplate.delete(TOKEN_KEY+telephone);
        } catch (Exception e){
            logger.error(e.getMessage());
            return Return.fail(1,"登出失败");
        }
        return Return.ok();
    }

    @Transactional
    public Return update1(HttpServletRequest request, User newMessage) {
        String token = JwtUtil.getTokenFromCookie(request);
        String telephone = JwtUtil.getUsername(token);
        try {
            cacheDataBaseUtil.updateById(USER_KEY,telephone,newMessage,(telephone2,newMessage2)->userMapper.updateUser(telephone2,newMessage2));
            return Return.ok();
        }catch (Exception e){
            logger.error(e.getMessage());
            throw new RuntimeException(e);
        }
    }

    @Transactional
    public Return updatePassword1(HttpServletRequest request, User newMessage) {
        String token = JwtUtil.getTokenFromCookie(request);
        String telephone = JwtUtil.getUsername(token);
        try {
            cacheDataBaseUtil.updateById(USER_KEY,telephone,newMessage,(telephone2,newMessage2)->userMapper.updatePassword(telephone2,newMessage2));
            return Return.ok();
        }catch (Exception e){
            logger.error(e.getMessage());
            throw new RuntimeException(e);
        }
    }
}
